//
//  OYVisitorView.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/1.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

class OYVisitorView: UIView {

    // 旋转图
    private lazy var rotateView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮罩图
    private lazy var maskingImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        
        label.numberOfLines = 0
        return label
    }()
    // 登录button
    private lazy var registerButton: UIButton = {
        let registerButton = UIButton()
        registerButton.setTitle("注册", for: .normal)
        registerButton.setTitleColor(UIColor.orange, for: .normal)
        registerButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
//        registerButton.adjustsImageWhenHighlighted = false
        registerButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    
        registerButton.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        
        return registerButton
        
    }()
    // 注册button
    private lazy var loginButton: UIButton = {
        let loginButton = UIButton()
        loginButton.setTitle("登录", for: .normal)
        loginButton.setTitleColor(UIColor.darkGray, for: .normal)
        loginButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
//        loginButton.adjustsImageWhenHighlighted = false
        loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)

        loginButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        return loginButton
        
    }()

    
    var registerCallBack:(() -> ())?
    var loginCallBack:(() -> ())?
    
    func registerAction() {
        registerCallBack?()
    }
    
    func loginAction() {
        loginCallBack?()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
     // MARK: -子类调用方法布局自身的visitorView
    func updateVisitorView(labelTitle: String?,imageName: String?)  {
        if labelTitle != nil,imageName != nil {
            // 不是首页
            houseView.image = UIImage(named: imageName!)
            welcomeLabel.text = labelTitle!
            
            rotateView.isHidden = true
            
            
        }else {
            // 是首页
            homeAnimition()
        }
    }
    
    
     // MARK: -首页动画效果
    private func homeAnimition()  {
        let homeAnimation = CABasicAnimation(keyPath: "transform.rotation")
        homeAnimation.fromValue = 0
        homeAnimation.toValue = M_PI * 2
        homeAnimation.duration = 20
        homeAnimation.repeatCount = MAXFLOAT
        homeAnimation.isRemovedOnCompletion = false
        
        self.rotateView.layer.add(homeAnimation, forKey: "homeAni")
    }
    
     // MARK: -创建子控件并布局
    private func setupUI() {
        
        self.backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        self.addSubview(rotateView)
        
        self.addSubview(maskingImageView)
        
        self.addSubview(houseView)

        self.addSubview(welcomeLabel)
        
        self.addSubview(registerButton)
  
        self.addSubview(loginButton)
        
        
        rotateView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        maskingImageView.snp_makeConstraints { (make) in
            make.center.equalTo(rotateView)
        }
        
        houseView.snp_makeConstraints { (make) in
            make.center.equalTo(rotateView)
        }
        
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.width.equalTo(224)
            make.top.equalTo(rotateView.snp_bottom).offset(20)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.left.equalTo(welcomeLabel)
            make.top.equalTo(welcomeLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 85, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.right.equalTo(welcomeLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
